package com.xncoding.starter.configure;

import com.xncoding.starter.inter.Reading;
import com.xncoding.starter.properties.ReadingProperties;
import com.xncoding.starter.service.ReadingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(ReadingProperties.class)
@ConditionalOnProperty(name = "reading.enable", havingValue = "true")  // 当存在reading.enable属性，且其值为true时，才初始化该Bean
public class ReadingConfiguration {
    @Autowired
    private ReadingProperties readingConfig;

    // 若当前IOC容器中没有Reading接口实现时，提供一个默认的Reading实现
    @Bean
    @ConditionalOnMissingBean(Reading.class)
    public Reading readingService() {
        return new ReadingService(this.readingConfig);
    }

}